package com.rh.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.entity.User;
import com.rh.mapper.UserMapper;
import com.rh.result.ResultVo;
import com.rh.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表 服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User selectByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("username", username));
    }

    /**
     * 条件查询带分页
     * @param page
     * @param limit
     * @param userQueryVo
     * @return
     */
    @Override
    public ResultVo pageQuery(Long page, Long limit, User userQueryVo) {
        //构造分页条件
        Page<User>page1=new Page<>(page,limit);
        QueryWrapper<User>wrapper=new QueryWrapper<>();
        if(userQueryVo==null){
            return this.findTeacherPage(page,limit);
        }
        //根据名称进行模糊查询
        if(!StringUtils.isBlank(userQueryVo.getUsername())){
            wrapper.like("username",userQueryVo.getUsername());
        }
        //调用分页查询
        baseMapper.selectPage(page1,wrapper);
        //总数
        long total = page1.getTotal();
        //总数居
        List<User> records = page1.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);
    }
    public ResultVo findTeacherPage(Long current, Long limit) {
        Page<User>page=new Page<>(current,limit);
        baseMapper.selectPage(page,null);
        long total = page.getTotal();
        List<User> records = page.getRecords();
        Map<String,Object> map=new HashMap<>();
        map.put("items",total);
        map.put("rows",records);
        return ResultVo.ok().data(map);
    }
}
